package com.smarth.zhizulian.jsinter;

import android.content.Context;
import android.webkit.JavascriptInterface;

import com.smarth.zhizulian.activity.LoginActivity;
import com.smarth.zhizulian.bean.video.VideoShare;
import com.smarth.zhizulian.http.KumaHttp;
import com.smarth.zhizulian.http.observer.ProgressObserver;
import com.smarth.zhizulian.http.utils.RxCompos;
import com.smarth.zhizulian.utils.KumaLog;
import com.smarth.zhizulian.utils.KumaToast;
import com.smarth.zhizulian.utils.ShareUtils;
import com.smarth.zhizulian.utils.UserUtils;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;

/**
 * Created by 18865 on 2018/1/26.
 */

public class JSVideoInterface {
    private Context mContext;
    public JSVideoInterface(Context context) {
        this.mContext = context;
    }

    @JavascriptInterface
    public void shareTimeLine(long relationId) {
        KumaLog.kuma("relationId:" + relationId);
        //朋友圈
        if (UserUtils.isLogin()) {
            KumaHttp.getService().videoShare("" + relationId)
                    .compose(RxCompos.<VideoShare>getBELoginCompos())
                    .subscribe(new ProgressObserver<VideoShare>(mContext) {
                        @Override
                        public void onNext(VideoShare videoShare) {
                            ShareUtils.shareToMomentWebPage(videoShare.getTitle(), videoShare.getDesc(), videoShare.getImage(), videoShare.getUrl(), new PlatformActionListener() {
                                @Override
                                public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                                    KumaToast.show(mContext, "分享成功");
                                }

                                @Override
                                public void onError(Platform platform, int i, Throwable throwable) {
                                    KumaToast.show(mContext, "分享失败");
                                }

                                @Override
                                public void onCancel(Platform platform, int i) {
                                    KumaToast.show(mContext, "取消分享");
                                }
                            });
                        }
                    });
        } else {
            LoginActivity.open(mContext);
        }
    }

    @JavascriptInterface
    public void shareFriend(long relationId) {
        KumaLog.kuma("relationId:" + relationId);
        //微信
        if (UserUtils.isLogin()) {
            KumaHttp.getService().videoShare("" + relationId)
                    .compose(RxCompos.<VideoShare>getBELoginCompos())
                    .subscribe(new ProgressObserver<VideoShare>(mContext) {
                        @Override
                        public void onNext(VideoShare videoShare) {
                            ShareUtils.shareToWechatWebPage(videoShare.getTitle(), videoShare.getDesc(), videoShare.getImage(), videoShare.getUrl(), new PlatformActionListener() {
                                @Override
                                public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                                    KumaToast.show(mContext, "分享成功");
                                }

                                @Override
                                public void onError(Platform platform, int i, Throwable throwable) {
                                    KumaToast.show(mContext, "分享失败");
                                }

                                @Override
                                public void onCancel(Platform platform, int i) {
                                    KumaToast.show(mContext, "取消分享");
                                }
                            });
                        }
                    });
        } else {
            LoginActivity.open(mContext);
        }
    }
}
